package com.shangyitong.yygh_hosp.controller.api;

import com.shangyitong.Result;
import com.shangyitong.ResultCodeEnum;
import com.shangyitong.exception.HospitalException;
import com.shangyitong.helper.HttpRequestHelper;
import com.shangyitong.yygh.model.hosp.Department;
import com.shangyitong.yygh.model.hosp.Hospital;
import com.shangyitong.yygh.model.hosp.HospitalSet;
import com.shangyitong.yygh.vo.hosp.DepartmentVo;
import com.shangyitong.yygh.vo.hosp.HospitalQueryVo;
import com.shangyitong.yygh.vo.hosp.ScheduleOrderVo;
import com.shangyitong.yygh_hosp.repository.DepartmentRepository;
import com.shangyitong.yygh_hosp.repository.HospitalRepository;
import com.shangyitong.yygh_hosp.service.DepartmentService;
import com.shangyitong.yygh_hosp.service.HospitalRearService;
import com.shangyitong.yygh_hosp.service.ScheduleService;
import com.shangyitong.yygh_hosp.service.api.HospitalService;
import com.shangyitong.yygh_hosp.service.HospitalSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.coyote.http11.filters.SavedRequestInputFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@Slf4j
@RequestMapping("/api/hosp")
@Api(tags = "医院管理API接口")
public class HospitalController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalRearService hospitalRearService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;
    /**
     * 上传医院
     * finished
     * @param request
     * @return
     */
    @ApiOperation(value = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        hospitalService.save(parameterMap);
        return Result.ok();
    }

    /**
     * 获取医院信息
     * finished
     * @param request
     * @return
     */
    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result showHospital(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Hospital hospital = hospitalService.getByHoscode(parameterMap);
        return Result.ok(hospital);
    }

    /**
     * yygh-site获取分页列表
     * finished
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    @ApiOperation(value = "获取分页列表")
    @GetMapping("hospital/findHospList/{page}/{limit}")
    public Result findHospList(@PathVariable Integer page,
                               @PathVariable Integer limit,
                               HospitalQueryVo hospitalQueryVo) {
        Page<Hospital> hospitals = hospitalRearService.showHospitalList(page, limit, hospitalQueryVo);
        return Result.ok(hospitals);
    }

    /**
     * 前台 根据名称查询所在医院
     * finished
     * @param hosname
     * @return
     */
    @ApiOperation(value = "根据名称查找所在医院")
    @GetMapping("hospital/findByHosname/{hosname}")
    public Result findByHosname(@PathVariable String hosname) {
        List<Hospital> hospitals = hospitalService.getByHosname(hosname);
        return Result.ok(hospitals);
    }

    /**
     * 查询医院详细信息
     * finished
     * @param hoscode
     * @return
     */
    @ApiOperation(value = "前台,查询该医院详细信息")
    @GetMapping("hospital/findHospDetail/{hoscode}")
    public Result findHospDetail(@PathVariable String hoscode) {
        Map<String, Object> objectMap = hospitalService.findHospDetail(hoscode);
        return Result.ok(objectMap);
    }

    /**
     * 查询该医院的部门信息
     * finished
     * @param hoscode
     * @return
     */
    @ApiOperation(value = "前台,查询该医院部门信息")
    @GetMapping("hospital/department/{hoscode}")
    public Result findDepartment(@PathVariable String hoscode) {
        List<DepartmentVo> departments = departmentService.getDeptList(hoscode);
        if (departments != null) {
            return Result.ok(departments);
        }
        return Result.fail();
    }

    /**
     * 根据排班id获取预约下单数据
     * @param scheduleId
     * @return
     */
    @ApiOperation(value = "根据排班id获取预约下单数据")
    @GetMapping("hospital/inner/getScheduleOrderVo/{scheduleId}")
    public ScheduleOrderVo getScheduleOrderVo(@PathVariable("scheduleId") String scheduleId) {
        log.info("scheduleId" + scheduleId);
        return scheduleService.getScheduleOrderVo(scheduleId);
    }
}
